<template>
  <div>
    <div style="padding:20px 10px; border-bottom:1px solid #ebebeb">
      <span>赛区列表</span>
    </div>
    <el-col class="add-btn" :span="4">
      <el-button type="primary" @click=";centerDialogVisible1 = true" :disabled="is_superuser">新增</el-button>
    </el-col>

    <template>
      <el-table border :data="areaData" style="width: 100%">
        <el-table-column type="index" :index="indexMethod" align="center"></el-table-column>
        <el-table-column label="赛区名称" prop="area_name" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              :disabled="is_superuser"
              @click="handleClick(scope.row);centerDialogVisible = true"
            >编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              :disabled="is_superuser"
              @click="prohibit(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>

    <!-- 编辑弹框 -->
    <el-dialog title="编辑信息" :visible.sync="centerDialogVisible" width="30%" center>
      <div class="form-list">
        <el-form ref="editForm" :model="editForm" label-width="80px">
          <el-form-item label="赛区名称">
            <el-input v-model="editForm.area_name"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit(editForm); centerDialogVisible = false">提交</el-button>
      </span>
    </el-dialog>

    <!-- 新增弹框 -->
    <el-dialog title="编辑信息" :visible.sync="centerDialogVisible1" width="30%" center>
      <div class="form-list">
        <el-form ref="form1" :model="form1" label-width="80px">
        <el-form-item label="赛区名称">
          <el-input v-model="form1.area_name"></el-input>
        </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="addsubtn(); centerDialogVisible1 = false">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>



<script>
import { log } from "util";
export default {
  name: "match",
  data() {
    return {
      // 新增弹框默认状态
      centerDialogVisible: false,
      centerDialogVisible1: false,

      areaData: [],
      is_superuser: true
        ? this.$store.state.is_superuser == "false"
        : this.$store.state.is_superuser == "true",
      // 编辑列表信息
      editForm: {
        area_id: "",
        area_name: ""
      },
      // 新增列表信息
      form1: {
        area_name: ""
      }
    };
  },

  mounted: function() {
    this.areaListInfo();
  },

  methods: {
    // 获取举办赛区信息
    areaListInfo() {
      let that = this;
      that.$api
        .getAreaInfo()
        .then(res => {
          that.areaData = res.data;
        })
        .catch(err => {});
    },

    // ===========================================================

    // 编辑赛区--获取行数据
    handleClick(row) {
      let that = this;
      that.editForm.area_id = row.id;
      that.editForm.area_name = row.area_name;
      // console.log(that.editForm)
    },

    // 编辑赛区-数据提交

    onSubmit(form) {
      let that = this;
      let postData = this.$qs.stringify({
        area_name: form.area_name
      });
      that.$api
        .editAreaInfo(form.area_id, postData)
        .then(res => {
          that.areaListInfo();
          that.$message({
            type: "success",
            message: "修改成功!"
          });
        })
        .catch(err => {
          that.$message({
            type: "error",
            message: "修改失败!"
          });
        });
    },

    // ===========================================================

    //  新增赛区
    addsubtn() {
      let that = this;
      let PostData = this.$qs.stringify({
        area_name: that.form1.area_name
      });
      that.$api
        .addAreaInfo(PostData)
        .then(res => {
          // console.log(res);
          if (res.status == 201) {
            // console.log(res.data);
            that.areaData.unshift(res.data);
            that.$message({
              type: "success",
              message: "新增成功!"
            });
          }
        })
        .catch(err => {});
    },

    // ============================================================

    //   删除按钮
    prohibit(obj) {
      console.log(obj);
      let that = this;
      that.$api
        .deleteAreaInfo(obj.id, { is_active: 0 })
        .then(res => {
          if (res.status == 200) {
            that.areaListInfo();
            that.$message({
              type: "success",
              message: "删除成功!"
            });
          }
        })
        .catch(err => {});
    },

    // 索引排序
    indexMethod(index) {
      return index * 1;
    }
  }
};
</script>

<style scoped>
.add-btn {
  margin: 30px 30px 30px 10px;
  padding-right: 120px;
}
</style>


